Two methods having the same implementation are suspicious. It might be that something else was intended. Or the duplication is intentional, which
becomes a maintenance burden.
class A {
private const CODE = "secret";
public function getCode() {
doTheThing();
return A::CODE;
}
public function getName() { // Noncompliant: duplicates getCode
doTheThing();
return A::CODE;
}
}
If the identical logic is intentional, the code should be refactored to avoid duplication. For example, by having both methods call the same method
or by having one implementation invoke the other.
class A {
private const CODE = "secret";
public function getCode() {
doTheThing();
return A::CODE;
}
public function getName() { // Intent is clear
return $this->getCode();
}
}
Exceptions
Methods that are not accessors (getters and setters), with fewer than 2 statements are ignored.
Additionally, accessors consisting of a single statement are ignored, if this statement does one of the following:
- Return
null
or an empty list []
- Throw an exception
- Return a newly created object